/*
    Copyright (c);2019-2022;Wiscom System;

	All rights reserved.

    Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

    1. Redistributions of source code must retain the above copyright notice, this
       list of conditions and the following disclaimer.
    2. Redistributions in binary form must reproduce the above copyright notice,
       this list of conditions and the following disclaimer in the documentation
       and/or other materials provided with the distribution.
    3. Neither the name of the Wiscom System nor the names of its contributors
       may be used to endorse or promote products derived from this software without
       specific prior written permission.

    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
OF SUCH DAMAGE.
*/

#include "los_sem.h"
#include "los_base.ph"
#include "los_hwi.h"
#include "los_api_sem.h"
#include "los_inspect_entry.h"
#ifdef LOSCFG_LIB_LIBC
#include "string.h"
#endif

#ifdef __cplusplus
#if __cplusplus
extern "C" {
#endif /* __cpluscplus */
#endif /* __cpluscplus */

/* task prio */
#define TASK_PRIO_TEST  5

/* task pid */
static UINT32 g_TestTaskID01, g_TestTaskID02, g_TestTaskID03;
/* sem id */
/* mux handle id */
static UINT32 g_Testmux01;
static UINT32 g_Testmux02;
static UINT32 g_Testmux03;
static UINT8 Task_finish_flag=0;
static VOID Example_MuxTask1(VOID)
{
    UINT32 uwRet;

    dprintf("task1_lo try get Mux g_Testmux02 LOS_WAIT_FOREVER .\r\n");
    /* get Mux, timeout */
    uwRet = LOS_MuxPend(g_Testmux02, LOS_WAIT_FOREVER);

    /* get Mux ok */
    if (LOS_OK == uwRet)
    {
        dprintf("task1_lo get Mux g_Testmux02 ok.\r\n");        
    }    
    /* timeout, get Mux fail */
    else
    {    
        dprintf("task1_lo get Mux g_Testmux02 fail.\r\n");        
    }

    LOS_TaskDelay(200);

    dprintf("task1_lo try get Mux g_Testmux01 LOS_WAIT_FOREVER .\r\n");
    uwRet = LOS_MuxPend(g_Testmux01, LOS_WAIT_FOREVER);

    /* get Mux ok */
    if (LOS_OK == uwRet)
    {
        dprintf("task1_lo get Mux g_Testmux01 ok.\r\n");        
    }    
    /* timeout, get Mux fail */
    else
    {    
        dprintf("task1_lo get Mux g_Testmux01 fail.\r\n");        
    }
    LOS_MuxPost(g_Testmux02);
    dprintf("task1_lo dead lock test success.\r\n");  
    LOS_MuxDelete(g_Testmux01);
    Task_finish_flag++;
       
    LOS_TaskDelete(g_TestTaskID01);
    
    return;
}

static VOID Example_MuxTask2(VOID)
{
    UINT32 uwRet;
    dprintf("task2_mi try get Mux g_Testmux01 timeout WAIT_FOREVER .\r\n");
    /* get Mux, timeout */
    uwRet = LOS_MuxPend(g_Testmux01, LOS_WAIT_FOREVER);

    /* get Mux ok */
    if (LOS_OK == uwRet)
    {
        dprintf("task2_mi get g_Testmux01 Mux ok.\r\n");        
    }    
    else
    {
        dprintf("task2_mi get Mux g_Testmux01 fail.\r\n");        
    }
    dprintf("task2_mi delay 100 ticks!\r\n");
    /* task delay 100 ticks */
    LOS_TaskDelay(100);

    dprintf("task2_mi try get Mux g_Testmux02 timeout WAIT_FOREVER .\r\n");
    /* get Mux, timeout */
    uwRet = LOS_MuxPend(g_Testmux02, LOS_WAIT_FOREVER);

    /* get Mux ok */
    if (LOS_OK == uwRet)
    {
        dprintf("task2_mi get Mux g_Testmux02 ok.\r\n");        
    }    
    else
    {
        dprintf("task2_mi get Mux g_Testmux02 fail.\r\n");        
    }
    LOS_MuxPost(g_Testmux01);
    dprintf("task2_mi dead lock test success.\r\n");  
    LOS_MuxDelete(g_Testmux02);
    //dprintf("task2_mi exit.\r\n"); 
    Task_finish_flag++;
    LOS_TaskDelete(g_TestTaskID02);
    
    return;
}
static VOID Example_MuxTask3(VOID)
{
    UINT32 uwRet;
    /* task delay 100 ticks */
    LOS_TaskDelay(3000);
    for(uwRet=0;uwRet<5;uwRet++)
    {
        dprintf("task3_hi run .\r\n");
        delay_1ms(1000);
    }
    Task_finish_flag++;
    LOS_MuxDelete(g_Testmux01);
    LOS_MuxDelete(g_Testmux02);
    LOS_TaskDelete(g_TestTaskID03);
    
    return;
}
UINT32 Example_MutexLock_deadlock_test(VOID)
{
    UINT32 uwRet = LOS_OK;
    TSK_INIT_PARAM_S stTask1;
    TSK_INIT_PARAM_S stTask2;    
    TSK_INIT_PARAM_S stTask3;

    dprintf("\r\n--------Example_Deadlock_Mutex test start!--------\r\n");
    /* create mutex */
    LOS_MuxCreate(&g_Testmux01);
    LOS_MuxCreate(&g_Testmux02);
    dprintf("MutexCreate success!\r\n");

    /* lock task schedue */
    LOS_TaskLock();

    /* create task1 */
    memset(&stTask1, 0, sizeof(TSK_INIT_PARAM_S));
    stTask1.pfnTaskEntry = (TSK_ENTRY_FUNC)Example_MuxTask1;
    stTask1.pcName       = "MutexTsk1";
    stTask1.uwStackSize  = LOSCFG_BASE_CORE_TSK_DEFAULT_STACK_SIZE;
    stTask1.usTaskPrio   = TASK_PRIO_TEST;
    uwRet = LOS_TaskCreate(&g_TestTaskID01, &stTask1);
    if (uwRet != LOS_OK)
    {
        dprintf("task1 create failed .\r\n");
        return LOS_NOK;
    }
    dprintf("Create Example_MutexTask1_Lo success!\r\n");

    /* create task2 */
    memset(&stTask2, 0, sizeof(TSK_INIT_PARAM_S));
    stTask2.pfnTaskEntry = (TSK_ENTRY_FUNC)Example_MuxTask2;
    stTask2.pcName       = "MutexTsk2";
    stTask2.uwStackSize  = LOSCFG_BASE_CORE_TSK_DEFAULT_STACK_SIZE;
    stTask2.usTaskPrio   = (TASK_PRIO_TEST - 1);
    uwRet = LOS_TaskCreate(&g_TestTaskID02, &stTask2);
    if (uwRet != LOS_OK)
    {
        dprintf("task2 create failed .\r\n");

        /* delete task 1 */
        if (LOS_OK != LOS_TaskDelete(g_TestTaskID01))
        {
            dprintf("task1 delete failed .\r\n");
        }

        return LOS_NOK;
    }
    dprintf("Create Example_MutexTask2_mi success!\r\n");

    /* create task3 */
    memset(&stTask3, 0, sizeof(TSK_INIT_PARAM_S));
    stTask3.pfnTaskEntry = (TSK_ENTRY_FUNC)Example_MuxTask3;
    stTask3.pcName       = "MutexTsk3";
    stTask3.uwStackSize  = LOSCFG_BASE_CORE_TSK_DEFAULT_STACK_SIZE;
    stTask3.usTaskPrio   = (TASK_PRIO_TEST - 2);
    uwRet = LOS_TaskCreate(&g_TestTaskID03, &stTask3);
    if (uwRet != LOS_OK)
    {
        dprintf("task3 create failed .\r\n");

        /* delete task 1 */
        if (LOS_OK != LOS_TaskDelete(g_TestTaskID01))
        {
            dprintf("task1 delete failed .\r\n");
        }

        return LOS_NOK;
    }
    dprintf("Create Example_MutexTask3_hi success!\r\n");
    Task_finish_flag=0;
    /* unlock task schedue */
    LOS_TaskUnlock();

    /* task delay 200 ticks */
    LOS_TaskDelay(200);
    while(Task_finish_flag<1)
    {
        LOS_TaskDelay(200);
    }
    Task_finish_flag=0;
    return uwRet;
}


#ifdef __cplusplus
#if __cplusplus
}
#endif /* __cpluscplus */
#endif /* __cpluscplus */
